package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbProd;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.ProdPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【tb_prod】的数据库操作Service实现
* @createDate 2025-05-27 15:34:28
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;
    @Override
    public Result getProdList(ProdPageVo prodPageVo) {
        Page<TbProd> page = new Page<>(prodPageVo.getPageNum(),prodPageVo.getPageSize());
        Page<TbProd> tbProdPage=tbProdMapper.selectProdList(page,prodPageVo);
        return Result.ok().setData(tbProdPage);
    }

    @Override
    public Result addProd(TbProd tbProd) {
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu()));
        if(prod!=null){
            return Result.error().setMsg("SPU商品已存在");
        }
        prod=tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName,tbProd.getProdName()));
        if(prod!=null){
            return Result.error().setMsg("同名商品已存在");
        }
        tbProd.setCreateTime(new Date());
        tbProd.setProdStatus(0);
        tbProd.setCreateBy("admin");
        tbProdMapper.insert(tbProd);
        return Result.ok();
    }

    @Override
    public Result deleteProdAll(List<TbProd> prodList) {
        for (TbProd tbProd : prodList) {
            tbProdMapper.deleteById(tbProd.getProdId());
        }
        return Result.ok();
    }
}




